package cn.heygod.airsql.util;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @className: SqlUtil
 * @author: Simon
 * @description: TODO
 * @date: 2024/1/26 10:48
 * @version: 1.0
 */
public class SqlUtil {

    public static void buildInString(StringBuilder stringBuilder,Object value, List<Object> params){
        buildInString(" IN ",stringBuilder,value,params);
    }

    public static void buildNotInString(StringBuilder stringBuilder,Object value, List<Object> params){
        buildInString(" NOT IN ",stringBuilder,value,params);
    }

    private static void buildInString(String inStr,StringBuilder stringBuilder,Object value, List<Object> params){
        stringBuilder.append(inStr);
        if(value instanceof Collection){
            Collection values = (Collection) value;
            stringBuilder.append(" ( ");
            stringBuilder.append( values.stream().map(v->" ? ").collect(Collectors.joining(", ")) );
            stringBuilder.append(" ) ");
            //填充参数值
            params.addAll(values);
        } else if (value.getClass().isArray()) {
            //转成List继续
            buildInString(stringBuilder,Arrays.asList((Object[]) value ),params);
        }else {
            //如果不是数组或者List类型，则默认为单个元素
            stringBuilder.append(" ( ? ) ");
            params.add(value);
        }
    }
}
